package com.ruoyi.outbound.service.impl;

import java.util.List;

import com.ruoyi.inventory.domain.Inventory;
import com.ruoyi.inventory.service.impl.InventoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.outbound.mapper.OutboundMapper;
import com.ruoyi.outbound.domain.Outbound;
import com.ruoyi.outbound.service.IOutboundService;

/**
 * 出库Service业务层处理
 * 
 * @author wyj
 * @date 2025-04-18
 */
@Service
public class OutboundServiceImpl implements IOutboundService 
{
    @Autowired
    private OutboundMapper outboundMapper;


    @Autowired
    private InventoryServiceImpl inventoryService;
    /**
     * 查询出库
     * 
     * @param outboundId 出库主键
     * @return 出库
     */
    @Override
    public Outbound selectOutboundByOutboundId(Long outboundId)
    {
        return outboundMapper.selectOutboundByOutboundId(outboundId);
    }

    /**
     * 查询出库列表
     * 
     * @param outbound 出库
     * @return 出库
     */
    @Override
    public List<Outbound> selectOutboundList(Outbound outbound)
    {
        return outboundMapper.selectOutboundList(outbound);
    }

    /**
     * 新增出库
     * 
     * @param outbound 出库
     * @return 结果
     */
    @Override
    public int insertOutbound(Outbound outbound)
    {

            try {
                //减少库存
                //锁库存
                Inventory inventory = new Inventory();
                //先查库存
                Inventory nowInventory = inventoryService.selectInventoryByInventoryId(outbound.getInventoryId());
                if (outbound.getStockQuantity()>nowInventory.getStockQuantity()) throw new Exception("库存不足");

                long num=nowInventory.getStockQuantity()-outbound.getStockQuantity();
                //合并库存
                inventory.setStockQuantity(num);
                inventory.setInventoryId(outbound.getInventoryId());
                inventoryService.updateInventory(inventory);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        return outboundMapper.insertOutbound(outbound);
    }

    /**
     * 修改出库
     * 
     * @param outbound 出库
     * @return 结果
     */
    @Override
    public int updateOutbound(Outbound outbound)
    {
        return outboundMapper.updateOutbound(outbound);
    }

    /**
     * 批量删除出库
     * 
     * @param outboundIds 需要删除的出库主键
     * @return 结果
     */
    @Override
    public int deleteOutboundByOutboundIds(Long[] outboundIds)
    {
        return outboundMapper.deleteOutboundByOutboundIds(outboundIds);
    }

    /**
     * 删除出库信息
     * 
     * @param outboundId 出库主键
     * @return 结果
     */
    @Override
    public int deleteOutboundByOutboundId(Long outboundId)
    {
        return outboundMapper.deleteOutboundByOutboundId(outboundId);
    }
}
